package com.green.web.controller.common;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.green.common.Constant;
import com.green.model.manage.Org;
import com.green.service.common.ServiceContext;
import com.green.service.manage.OrgService;

public class BaseController   {
	@Autowired
	private OrgService orgService;
	public final String JEE_REQUEST = "request";
	public final String  JEE_SESSION = "session";
	public final String  JEE_APPLICATION = "application";
	
	public String getString(String name) {
		return getString(name, null);
	}

	public String getString(String name, String defaultValue) {
		String resultStr = getRequest().getParameter(name);
		if (resultStr == null || "".equals(resultStr)
				|| "null".equals(resultStr) || "undefined".equals(resultStr)) {
			return defaultValue;
		} else {
			return resultStr.trim();
		}
	}

	public void outPrint(HttpServletResponse response, Object result) {
        try {
            response.setCharacterEncoding("utf-8");
            PrintWriter out = response.getWriter();
            out.print(result.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
	
	public String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
    
	/**

	 * @param <T>
	 * @param key
	 * @param scop
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> T getBean(String key, String scop) {
		HttpServletRequest req = this.getRequest();
		T t = null;
		if (null == scop) {
			t = (T) req.getAttribute(key);
			if (null == t)
				t = (T) req.getSession().getAttribute(key);
			if (null == t)
				t = (T) getServletContext().getAttribute(key);
		} else {

			if (this.JEE_REQUEST.equals(scop)) {
				t = (T) req.getAttribute(key);
			} else if (this.JEE_SESSION.equals(scop)) {
				t = (T) req.getSession().getAttribute(key);
			} else if (this.JEE_APPLICATION.equals(scop)) {
				t = (T) getServletContext().getAttribute(key);
			}
		}
		return t;
	}
	/**
	 * 
	 * lineshow  created on 2012-3-15
	 * @param <T>
	 * @param key
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> T getBeanInReq(String key) {
		return (T)getBean(key,this.JEE_REQUEST);
	}

	/**
	 * 放入内置对象
	 * @param key
	 * @param obj
	 * @param scop
	 */
	public void setBean(String key, Object obj, String scop) {
		if (null == key) {
			key = this.JEE_REQUEST;
		}
		HttpServletRequest req = this.getRequest();
		if (this.JEE_REQUEST.equals(scop)) {
			req.setAttribute(key, obj);
		} else if (this.JEE_SESSION.equals(scop)) {
			req.getSession().setAttribute(key, obj);
		} else if (this.JEE_APPLICATION.equals(scop)) {
			this.getServletContext().setAttribute(key, obj);
		}
	}
	/**
	 * 
	 * lineshow  created on 2012-3-15
	 * @param key
	 * @param obj
	 */
	public void setBeanInReq(String key, Object obj) {
		setBean(key,obj,this.JEE_REQUEST);
	}
	
	public HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
	}

	public HttpSession getSession() {
		return ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest().getSession();
	}

	public ServletContext getServletContext() {
		return ContextLoader.getCurrentWebApplicationContext()
				.getServletContext();
	}

	public int getInt(String name) {
		return getInt(name, 0);
	}

	public int getInt(String name, int defaultValue) {
		String resultStr = getRequest().getParameter(name);
		if (resultStr != null) {
			try {
				return Integer.parseInt(resultStr);
			} catch (Exception e) {
				return defaultValue;
			}
		}
		return defaultValue;
	}

	public BigDecimal getBigDecimal(String name) {
		return getBigDecimal(name, null);
	}

	public BigDecimal getBigDecimal(String name, BigDecimal defaultValue) {
		String resultStr = getRequest().getParameter(name);
		if (resultStr != null) {
			try {
				return BigDecimal.valueOf(Double.parseDouble(resultStr));
			} catch (Exception e) {
				return defaultValue;
			}
		}
		return defaultValue;
	}
	public Double getDouble(String name) {
		return getDouble(name, null);
	}

	public Double getDouble(String name, Double defaultValue) {
		String resultStr = getRequest().getParameter(name);
		if (resultStr != null) {
			try {
				return Double.parseDouble(resultStr);
			} catch (Exception e) {
				return defaultValue;
			}
		}
		return defaultValue;
	}
	
	protected JsonConfig getDefaultJsonConfig(){
		JsonConfig config = new JsonConfig();
		config.registerJsonValueProcessor(Date.class, new JsonValueProcessor() {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			@Override
			public Object processObjectValue(String key, Object value, JsonConfig cfg) {
				if(value!=null && value instanceof Date){
					return sdf.format(value);
				}
				return null;
			}
			
			@Override
			public Object processArrayValue(Object value, JsonConfig cfg) {
				if(value!=null && value instanceof Date){
					return sdf.format(value);
				}
				return null;
			}
		});
		return config;
	}
	
	protected boolean isJoingreen(){
		Org org=orgService.getOrgById(ServiceContext.getLoginPerson().getControUnit().getId());
		if(org.getId().equals(Constant.JOIN_ID)){
    		return true;
    	}else{
    		return false;
    	}
	}
}
